package com.example.Arithmetic.Lettcode;

import com.example.Arithmetic.Arithmetic.TreeNode;

/**
 * 日期：2023/10/18
 * 时间：11:54
 * 描述：寻找二叉搜索树的最近公共祖先
 */
public class E04Lettcode235 {
    //迭代
    public TreeNode lowestCommonAncestor(TreeNode<Integer> root, TreeNode<Integer> p, TreeNode<Integer> q) {
        TreeNode<Integer> a = root;
        while (p.val < a.val && q.val < a.val || q.val > a.val && q.val > a.val) {
            if (p.val < a.val) {
                a = a.left;
            } else {
                a = a.right;
            }
        }
        return a;
    }
}

